package pl.bizuteria.sklep.admin.client.ui.product;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryEditorDriver;
import com.google.web.bindery.requestfactory.shared.EntityProxy;

public abstract class ProductCreateView<P extends EntityProxy> extends
		Composite implements IProductCreateView<P> {

	
	private static ProductCreateViewUiBinder uiBinder = GWT
			.create(ProductCreateViewUiBinder.class);

	interface ProductCreateViewUiBinder extends
			UiBinder<Widget, ProductCreateView> {
	}
	
	public ProductCreateView(IProductProxyView<P> proxyView) {
		this.proxyView = proxyView;
		uiBinder.createAndBindUi(this);
	}
	
	@UiField(provided=true)
	IProductProxyView<P> proxyView;

	@UiField
	public Button saveButton;

	@UiField
	public Button cancelButton;

	@UiHandler("saveButton")
	public void saveClicked(ClickEvent e) {
		presenter.editClicked();
	}

	@UiHandler("cancelButton")
	public void cancelClicked(ClickEvent we) {
		presenter.cancelClicked();
	}

	private Presenter presenter;

	public void setPresenter(
			pl.bizuteria.sklep.admin.client.ui.product.IProductCreateView.Presenter presenter) {
		this.presenter = presenter;
	}

	public RequestFactoryEditorDriver<P, ? extends Editor<P>> createEditorDriver() {
		RequestFactoryEditorDriver<P, IProductProxyView<P>> rfEditorDriver = createSpecificEditorDriver();
		rfEditorDriver.initialize(proxyView);
		return rfEditorDriver;
	}

	protected abstract RequestFactoryEditorDriver<P, IProductProxyView<P>> createSpecificEditorDriver();
	
}
